//
//  UIViewController+MZBasic.m
//  MZBasic
//
//  Created by Sings on 2019/9/10.
//

#import "UIViewController+MZBasic.h"
#import "NSString+MZBasic.h"
#import "NavBarButton.h"

@implementation UIViewController (MZBasic)

- (void)setNavLeftButton:(nullable NSString *)imageName Title:(nullable NSString *)title {
    
    NavBarButton *navBtn = [NavBarButton buttonWithType:UIButtonTypeCustom];
    
    navBtn.frame = CGRectMake(0, 0, 40, 40);
    
    [navBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    [navBtn setTitleColor:[navBtn titleColor] forState:UIControlStateNormal];
    
    [navBtn addTarget:self
               action:@selector(navLeftButtonClick:)
     forControlEvents:UIControlEventTouchUpInside];
    
    [navBtn setTitle:title forState:UIControlStateNormal];
    
    navBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    UIBarButtonItem *navBackBar = [[UIBarButtonItem alloc] initWithCustomView:navBtn];
    
    self.navigationItem.leftBarButtonItem = navBackBar;
}


- (void)setNavRightButton:(nullable NSString *)imageName Title:(nullable NSString *)title {
    
    self.navigationItem.rightBarButtonItems = nil;
    
    self.navigationItem.rightBarButtonItem =
    [self outButtonWithImageName:imageName select:@selector(navRightButtonClick:) Title:title];
}

- (void)setNavRightButton:(nullable NSString *)imageName
              OtherButton:(nullable NSString *)OtherImageName
                    Title:(nullable NSString *)title
               OtherTitle:(nullable NSString *)otherTitle {
    
    self.navigationItem.rightBarButtonItem = nil;
    
    self.navigationItem.rightBarButtonItems = @[
                                                [self outButtonWithImageName:imageName select:@selector(navRightButtonClick:) Title:title],
                                                [self outButtonWithImageName:OtherImageName
                                                                      select:@selector(navRightOtherButtonClick:)
                                                                       Title:otherTitle]
                                                ];
}

- (UIBarButtonItem *)outButtonWithImageName:(nullable NSString *)imageName
                                     select:(SEL)action
                                      Title:(nullable NSString *)title {
    
    NavBarButton *navBtn = [NavBarButton buttonWithType:UIButtonTypeCustom];
    
    if (title != nil && ![title isEqualToString:@""]) {
        CGFloat width = [title basic_widthWithFont:[UIFont systemFontOfSize:14.] constrainedToHeight:44];
        if (ceil(width) > 44) {
            navBtn.frame = CGRectMake(0, 0, width, 44); // 40,40
        }else{
            navBtn.frame = CGRectMake(0, 0, 44, 44); // 40,40
        }
        
    } else {
        navBtn.frame = CGRectMake(0, 0, 25, 44); // 40,40
    }
    
    [navBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    
    [navBtn setTitleColor:[navBtn titleColor] forState:UIControlStateNormal];
    
    [navBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    [navBtn setTitle:title forState:UIControlStateNormal];
    
    navBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    UIBarButtonItem *navBackBar = [[UIBarButtonItem alloc] initWithCustomView:navBtn];
    
    return navBackBar;
}

- (void)navLeftButtonClick:(UIButton *)btn {
}

- (void)navRightButtonClick:(UIButton *)btn {
}

- (void)navRightOtherButtonClick:(UIButton *)btn {
}

@end
